//
//  DrugViewController.m
//  安好
//
//  Created by lianchuang on 14-11-13.
//  Copyright (c) 2014年 lianchuangbrother. All rights reserved.
//

#import "DrugViewController.h"
#import "DrugDetailViewController.h"
#import "EncyCell.h"
#import "DrugDViewController.h"
@interface DrugViewController ()

@end

@implementation DrugViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.encyArray = [[NSMutableArray alloc]init];
    [self makeNavBar];
    [self loadData];
    [self makeTableView];
    
}
- (void)loadData
{
    [self showActionView];
    NSMutableDictionary *dict = [[NSMutableDictionary alloc]initWithCapacity:10];
    
    NSString *isFromMyFocuse;
    if (self.isFromMyFoucus) {
        isFromMyFocuse = [NSString stringWithFormat:@"collection/list/"];
        [dict setObject:@"5" forKey:@"type"];
    }else{
        isFromMyFocuse = [NSString stringWithFormat:@"wiki/drugCategory"];
        [dict setObject:@"0" forKey:@"pid"];
    }
    
    [dict setObject:kPlatform forKey:@"platform"];
    [dict setObject:kVersion forKey:@"version"];
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager POST:[NSString stringWithFormat:@"%@%@",kBaseURL,isFromMyFocuse] parameters:dict success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSNumber * statusNum = [responseObject objectForKey:@"status"];
        NSNumberFormatter* numberFormatter = [[NSNumberFormatter alloc] init];
        NSString * statusString = [numberFormatter stringFromNumber:statusNum];
        [self hiddenActionView];
        if ([statusString isEqualToString:@"0"]) {
            [self.tableView footerEndRefreshing];
        }else{
            NSArray *jsonDictArr = [[responseObject objectForKey:@"data"] isEqual:[NSNull null]]?nil:[responseObject objectForKey:@"data"];
            
            MyNSLog("%@", jsonDictArr);
            if (jsonDictArr!=nil) {
                for (NSDictionary * dic in jsonDictArr){
                    [self.encyArray addObject:dic];
                }
                [self.tableView reloadData];
            }
            
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [self hiddenActionView];
    }];

}
#pragma mark - 导航条的布局
-(void)makeNavBar
{
    self.navigationItem.title = @"药品";
    //贴 返回 按钮的图片
    UIBarButtonItem *backImg = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"公用_返回.png"] style:UIBarButtonItemStylePlain target:self action:@selector(backClicked)];
    backImg.tintColor = [UIColor whiteColor];
    self.navigationItem.leftBarButtonItem = backImg;
}
-(void)backClicked
{
    [self.navigationController popViewControllerAnimated:YES];
}
#pragma mark - 创建tableView 设置table的代理、添加头视图横线
-(void)makeTableView
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight-iOS7TopMargin) style:UITableViewStylePlain];
    [self.view addSubview:self.tableView];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    //隐藏横向和竖向的滑条
    self.tableView.showsHorizontalScrollIndicator = NO;
    self.tableView.showsVerticalScrollIndicator = NO;
    [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    //    self.tableView.backgroundColor = [UIColor colorWithRed:242/255.0 green:242/255.0 blue:242/255.0 alpha:1];
    //创建头视图 - 一条线
    self.headerLineView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 0.5)];
    self.headerLineView.backgroundColor = [ClassMethodsViewController colorWithHexString:LINE_BACKGROUNDCOLOR_16];
    self.tableView.tableHeaderView = self.headerLineView;
    
}
#pragma mark - tableView的代理方法
/**
 *  设置 各行cell高
 */
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (kScreenWidth == 414){
        return 85;

    }else{
        
        return 64;
    }
}
/**
 *  设置 头视图的行高
 */
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.encyArray.count;
}
#pragma mark --cell
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    EncyCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ID"];
    if (cell == nil) {
        if (kScreenWidth == 414){
            cell = [[[NSBundle mainBundle] loadNibNamed:@"EncyCell" owner:self options:nil] lastObject];
            
        }else{
            cell = [[[NSBundle mainBundle] loadNibNamed:@"EncyCell" owner:self options:nil] firstObject];

        }
    }
    cell.itemImgView.layer.cornerRadius=3.0;
    cell.itemImgView.layer.masksToBounds=YES;
    if (indexPath.row == 0) {
        cell.itemImgView.image = [UIImage imageNamed:@"ChinaDurg@2x"];
    }else if (indexPath.row == 1){
        cell.itemImgView.image = [UIImage imageNamed:@"WesternDrug@2x"];
    }else{
        cell.itemImgView.image = [UIImage imageNamed:@"HealthDrug@2x"];
    }
    if (self.isFromMyFoucus) {
        cell.titleLable.text = [NSString stringWithFormat:@"%@",[[self.encyArray objectAtIndex:indexPath.row] objectForKey:@"drugname"]];
        cell.infoLable.text = [NSString stringWithFormat:@"%@种相关药品",[[self.encyArray objectAtIndex:indexPath.row] objectForKey:@"drugid"]];
    }else{
        cell.titleLable.text = [NSString stringWithFormat:@"%@",[[self.encyArray objectAtIndex:indexPath.row] objectForKey:@"catname"]];
        cell.infoLable.text = [NSString stringWithFormat:@"%@种相关药品",[[self.encyArray objectAtIndex:indexPath.row] objectForKey:@"count"]];
    }
    
    cell.totalLable.hidden = YES;
    //底线
    cell.bottomLineView.backgroundColor = [ClassMethodsViewController colorWithHexString:LINE_BACKGROUNDCOLOR_16];
    if (indexPath.row != self.encyArray.count - 1)
    {
        if (kScreenWidth == 414){
            cell.bottomLineView.frame = CGRectMake(88, 84.5,kScreenWidth-70 - 5, 0.3);

        }else{
            
            cell.bottomLineView.frame = CGRectMake(70, 63,kScreenWidth-70 - 5, 0.3);
        }
    }
    else
    {
        if (kScreenWidth == 414){
            cell.bottomLineView.frame = CGRectMake(0, 84.5, kScreenWidth, 0.3);

        }else{
            
            cell.bottomLineView.frame = CGRectMake(0, 63, kScreenWidth, 0.3);
        }
    }
    if (indexPath.row == 0) {
        cell.topLineView.backgroundColor =[ClassMethodsViewController colorWithHexString:LINE_BACKGROUNDCOLOR_16];
        cell.topLineView.frame = CGRectMake(0, 0, kScreenWidth, 0.3);
    }
    
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    DrugDetailViewController * circleSectionGradeViewController = [[DrugDetailViewController alloc]init];
    if (self.isFromMyFoucus) {
        circleSectionGradeViewController.catid = [[self.encyArray objectAtIndex:indexPath.row] objectForKey:@"drugname"];
        circleSectionGradeViewController.catname = [[self.encyArray objectAtIndex:indexPath.row] objectForKey:@"drugid"];
        
        DrugDViewController *drugVC = [[DrugDViewController alloc] init];
        drugVC.drugid = circleSectionGradeViewController.catname;
        drugVC.drugname = circleSectionGradeViewController.catid;
        [self.navigationController pushViewController:drugVC animated:YES];
        return;
        
    }else{
        circleSectionGradeViewController.catid = [[self.encyArray objectAtIndex:indexPath.row] objectForKey:@"catid"];
        circleSectionGradeViewController.catname = [[self.encyArray objectAtIndex:indexPath.row] objectForKey:@"catname"];
    }
    if (indexPath.row == 2) {
        circleSectionGradeViewController.isHealthCat = YES;
    }else{
        circleSectionGradeViewController.isHealthCat = NO;
        
    }
    [self.navigationController pushViewController:circleSectionGradeViewController animated:YES];
    
    
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
